<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admins extends MY_Controller {
	
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
		//if( $this->session->user_data('level') != 1 ){
		//	redirect('');
		//}
        $this->data['controller'] = 'admins';
        $this->load->model('Madmins');
    }
    
    /*
	* admin-system
	*/
    public function indexadminsystem()
	{
		$this->checkAdminSystem();
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Madmins->getListAdminSystem($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        // h phan trang cho list.
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên tài khoản', 'required|trim');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        $this->form_validation->set_rules('fullname', 'Họ tên', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('email', 'Địa chỉ email', 'required|trim|valid_email');
        if($this->form_validation->run() === TRUE)
        {
            $type = $this->getParamString('type');
            $arr = array(
                'username' => $this->getParamString('username'),
                'password' => $this->getParamString('password'),
                'fullname' => $this->getParamString('fullname'),
                'mobile' => $this->getParamString('mobile'),
                'email' => $this->getParamString('email')
            );
            if($type === 'add'){        
                $f = $this->Madmins->addAdminsystem($arr);
                
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Thêm thành công!");window.location="'.$this->data['baseurl'].'admin-system";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-system";</script>');
					}else{
						if($f == '202'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-system";</script>');
					}
					}
				}
            }elseif($type === 'edit'){
                $id = $this->getParamInt('id');
                $data = array(
					'username' => $this->getParamString('username'),					
					'fullname' => $this->getParamString('fullname'),
					'email' => $this->getParamString('email'),
					'mobile' => $this->getParamString('mobile')
					);
				$password = $this->getParamString('password1');
                $f = $this->Madmins->editAdminsystem($id,$data,$password);
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Cập nhật thành công!");window.location="'.$this->data['baseurl'].'admin-system";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-system";</script>');
					}else{
						if($f == '202'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-system";</script>');
					}
					}
				}
            }
            
        }
        //$this->data['province'] = $this->getProvince();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
    
	
	
    public function detailadminsystem()
    {
		$this->checkAdminSystem();
        $this->data['data'] = $this->Madmins->getAdminSystemDetail($this->getParamInt('id'));
        //$this->data['province'] = $this->getProvince();
        //$this->data['district'] = $this->getDistrictData($this->data['data']->id_province);
        $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/admins/detailadminsystem', $this->data);
    }
    
    public function deleteadminsystem()
    {
		$this->checkAdminSystem();
        $f = $this->Madmins->deleteAdminSystem($this->getParamInt('id'));
        if($f == '200'){
            //die('<meta charset="utf-8"><script type="text/javascript">alert("Xóa thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-system');
        }else{
			die('<meta charset="utf-8"><script type="text/javascript">alert("Bạn không thể xóa root!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-system');
		}
    }
	
	/*
	 admin-group
	*/
	public function indexadmingroup()
	{
		$this->checkAdminSystem();
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Madmins->getListAdminGroup($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        $this->data['getGroup'] = $this->Madmins->getGroup();
        // h phan trang cho list.
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên tài khoản', 'required|trim');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        $this->form_validation->set_rules('fullname', 'Họ tên', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('email', 'Địa chỉ email', 'required|trim|valid_email');
		$this->form_validation->set_rules('arr_group_id[]', 'Nhóm mạng', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $type = $this->getParamString('type');
            $arr = array(
                'username' => $this->getParamString('username'),
                'password' => $this->getParamString('password'),
                'fullname' => $this->getParamString('fullname'),
                'mobile' => $this->getParamString('mobile'),
                'email' => $this->getParamString('email'),
            );
            
            $arr_group_id = @$this->getParamArray('arr_group_id');
            if($type === 'add'){        
                $f = $this->Madmins->addAdminGroup($arr,$arr_group_id);
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Thêm thành công!");window.location="'.$this->data['baseurl'].'admin-group";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-group";</script>');
					}else{
						if($f == '202'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-group";</script>');
					}
					}
				}
            }elseif($type === 'edit'){
                $id = $this->getParamInt('id');
                $data = array(
					'username' => $this->getParamString('username'),					
					'fullname' => $this->getParamString('fullname'),
					'email' => $this->getParamString('email'),
					'mobile' => $this->getParamString('mobile'),
					);
				$password = $this->getParamString('password1');
                $f = $this->Madmins->editAdminGroup($id,$data,$password,$arr_group_id);
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Cập nhật thành công!");window.location="'.$this->data['baseurl'].'admin-group";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-group";</script>');
					}else{
						if($f == '202'){
							die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-group";</script>');
						}
					}
				}
            }
            
        }
        //$this->data['province'] = $this->getProvince();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
    
	
	
    public function detailadmingroup()
    {
		$this->checkAdminSystem();
        $this->data['data'] = $this->Madmins->getAdminGroupDetail($this->getParamInt('id'));
        $this->data['groupManaged'] = $this->Madmins->getListGroupManaged($this->getParamInt('id'));
        $this->data['getGroup'] = $this->Madmins->getGroup();
        $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/admins/detailadmingroup', $this->data);
    }
    
    public function deleteadmingroup()
    {
		$this->checkAdminSystem();
        $f = $this->Madmins->deleteAdminGroup($this->getParamInt('id'));
        if($f == '200'){
            //die('<meta charset="utf-8"><script type="text/javascript">alert("Xóa thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-group');
        }else{
			die('<meta charset="utf-8"><script type="text/javascript">alert("Bạn không thể xóa root!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-group');
		}
    }
	
    public function getDistrict()
    {
        die($this->data['fview']->selectOption($this->getDistrictData($this->getParamInt('id')), '', 'w99', 'district', 'district'));
    }
    
	/*
	 admin-device
	*/
	public function indexadmindevice()
	{
		if( !in_array($this->session->userdata('level'),array(1,2)) ){
			redirect('');
		}
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Madmins->getListAdminDevice($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        $this->data['getGroup'] = $this->Madmins->getGroup();
		$this->data['getDevice'] = $this->Madmins->getDevice();
        // h phan trang cho list.
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên tài khoản', 'required|trim');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        $this->form_validation->set_rules('fullname', 'Họ tên', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('email', 'Địa chỉ email', 'required|trim|valid_email');
		$this->form_validation->set_rules('manage_group_id', 'Nhóm mạng', 'required');
		$this->form_validation->set_rules('arr_device_id[]', 'Thiết bị', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $type = $this->getParamString('type');
            $arr = array(
                'username' => $this->getParamString('username'),
                'password' => $this->getParamString('password'),
                'fullname' => $this->getParamString('fullname'),
                'mobile' => $this->getParamString('mobile'),
                'email' => $this->getParamString('email'),
				'manage_group_id' =>$this->getParamInt('manage_group_id')
            );
			$arr_device_id = @$this->getParamArray('arr_device_id');
			if($type === 'add'){        
                $f = $this->Madmins->addAdminDevice($arr,$arr_device_id);
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Thêm thành công!");window.location="'.$this->data['baseurl'].'admin-device";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-device";</script>');
					}else{
						if($f == '202'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-device";</script>');
					}
					}
				}
            }elseif($type === 'edit'){
                $this->checkPermissionReg($this->getParamInt('id'));
                $data = array(
					'username' => $this->getParamString('username'),					
					'fullname' => $this->getParamString('fullname'),
					'email' => $this->getParamString('email'),
					'mobile' => $this->getParamString('mobile'),
					'manage_group_id' =>$this->getParamInt('manage_group_id')
					);
					$arr_device_id = $this->getParamArray('arr_device_id');
				$password = $this->getParamString('password1');
                $f = $this->Madmins->editAdminDevice($this->getParamInt('id'),$data,$password,$arr_device_id);
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Cập nhật thành công!");window.location="'.$this->data['baseurl'].'admin-device";</script>');
                }else{
					if($f == '201'){
						die('<meta charset="utf-8"><script type="text/javascript">alert("Tên đăng nhập tồn tại !");window.location="'.$this->data['baseurl'].'admin-device";</script>');
					}else{
						if($f == '202'){
							die('<meta charset="utf-8"><script type="text/javascript">alert("Email tồn tại !");window.location="'.$this->data['baseurl'].'admin-device";</script>');
						}
					}
				}
            }
            
        }
        //$this->data['province'] = $this->getProvince();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
 
    public function detailadmindevice()
    {
		$this->checkPermissionReg($this->getParamInt('id'));
        $this->data['data'] = $this->Madmins->getAdminDeviceDetail($this->getParamInt('id'));
        //echo "<pre>"; die(print_r($this->data['data']));
        $this->data['deviceManaged'] = $this->Madmins->getListDeviceManaged($this->getParamInt('id'));
        $this->data['getGroup'] = $this->Madmins->getGroup();
		$this->data['getDevice'] = $this->Madmins->getDevice($this->getParamInt('id'),$this->data['data']->manage_group_id);
        $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/admins/detailadmindevice', $this->data);
    }
    
    public function deleteadmindevice()
    {
		$this->checkPermissionReg($this->getParamInt('id'));
        $f = $this->Madmins->deleteAdminDevice($this->getParamInt('id'));
        if($f == '200'){
            //die('<meta charset="utf-8"><script type="text/javascript">alert("Xóa thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-device');
        }else{
			die('<meta charset="utf-8"><script type="text/javascript">alert("Bạn không thể xóa root!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('admin-device');
		}
    }
	
	public function getlistdevice(){
		$this->data['data'] = $this->Madmins->getlistdevice();
		$this->data['action'] = __FUNCTION__;
		$this->load->view('temp/admins/getlistdevice', $this->data);
	}
	
	public function resetformad(){
        $this->data['getGroup'] = $this->Madmins->getGroup();
		$this->data['getDevice'] = $this->Madmins->getDevice();
		$this->load->view('temp/admins/resetformad', $this->data);
	}
    
    public function resetformgroup(){
        $this->data['getGroup'] = $this->Madmins->getGroup();
		$this->load->view('temp/admins/resetformgroup', $this->data);
	}
}
